package com.uniview.plugins.bpm;

import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.orderType.ExternalTest;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.uniview.plugins.adapter.BMPDataSource;

import java.util.List;

@Service("bpmService")
public class BpmService {

	private Logger logger = Logger.getLogger(BpmService.class);

	@Autowired
	private BMPDataSource bpmDataSource;

	@Value("#{configProperties['bmp.SuperRole']}")
	private String SuperRoleTable;

	@Value("#{configProperties['bmp.ExternalTest']}")
	private String ExternalTestTable;

	private boolean checkIsSuperRole(String workNo) throws Exception {
		String countSql = "select count(1) from " + this.SuperRoleTable + " where CHARINDEX('" + workNo
				+ "', Member) > 0";
		Integer total = bpmDataSource.getCount(countSql);
		return total > 0;
	}

	public Pager<ExternalTest> findExternalTestByWorkNo(String workNo, String queryString, int page, int limit)
			throws Exception {
		// 处理域账号
		workNo = workNo.substring(1, workNo.length());
		StringBuilder builder = new StringBuilder();
		builder.append("select w2.n, w1.* from " + this.ExternalTestTable + " w1, (");
		builder.append("SELECT TOP " + (page * limit)
				+ " row_number () OVER (ORDER BY WF_LastModified desc) n, WF_DocNumber from " + this.ExternalTestTable
				+ "");
		boolean isSuperRole = this.checkIsSuperRole(workNo);
		if (isSuperRole) {
			builder.append(" where 1=1");
		} else {
			builder.append(" where (WF_AddName like '%" + workNo + "%' or WF_AllReaders like '%" + workNo + "%')");
		}

		if (StringUtils.isNotBlank(queryString)) {
			builder.append(" and (xiangmumc like '%" + queryString + "%' or kehumc like '%" + queryString + "%')");
		}
		builder.append(" ) w2 where w1.WF_DocNumber = w2.WF_DocNumber and w2.n > " + (page - 1) * limit);
		logger.debug("查询用户对外测试数据sql========" + builder.toString());
		List<ExternalTest> list = bpmDataSource.queryForList(ExternalTest.class, builder.toString());
		// 查询总记录数
		StringBuilder countBuilder = new StringBuilder();
		countBuilder.append("select count(1) from " + this.ExternalTestTable);
		if (isSuperRole) {
			countBuilder.append(" where 1=1 ");
		} else {
			countBuilder.append(" where (CHARINDEX(WF_AddName, '" + workNo + "') > 0 or CHARINDEX(WF_AllReaders, '"
					+ workNo + "') > 0)");
		}
		// String countSql = "select count(*) from " + this.ExternalTestTable + " where
		// INSTR(\"WF_AddName\"," + workNo + ") >0 ";
		if (StringUtils.isNotBlank(queryString)) {
			countBuilder.append(" and (xiangmumc like '%" + queryString + "%' or kehumc like '%" + queryString + "%')");
			// countSql += " and (\"xiangmumc\" like '%" + queryString + "%' or \"kehumc\"
			// like '%" + queryString + "%')";
		}
		logger.debug("统计用户对外测试数据sql========" + countBuilder.toString());
		int total = bpmDataSource.getCount(countBuilder.toString());

		Pager<ExternalTest> pager = new Pager<ExternalTest>();
		pager.setPagerList(list);
		pager.setTotalRow(total);
		return pager;
	}
}
